# @Time : 2024-11-04 17:28
# @Author : Fioman 
# @Phone : 13149920693
"""
多线程和多进程在针对IO操作的时候,他们的效率是差不多的,但是因为多进程会占用更多的资源,所以使用
多线程来进行IO操作是比多进程进行IO操作是更优的选择
"""
import threading
import time


def get_detail_html(url: str):
    currentThread = threading.current_thread()
    print("当前线程ID: {}, 线程名称: {}".format(currentThread.ident, currentThread.name))
    print(f"get detail html start")
    time.sleep(2)
    print(f"get detail html end")


def get_detail_url():
    currentThread = threading.current_thread()
    print("当前线程ID: {}, 线程名称: {}".format(currentThread.ident, currentThread.name))
    print(f"get detail url start")
    time.sleep(3)
    print(f"get detail url end")


if __name__ == '__main__':
    detailUrlThread = threading.Thread(target=get_detail_url, args=())
    detailUrlThread.deamon = True
    detailUrlThread.start()
    detailHtmlThread = threading.Thread(target=get_detail_html, args=("https://www.baidu.com",))
    detailHtmlThread.deamon = True
    detailHtmlThread.start()
    threadStartTime = time.time()
    currentThread_G = threading.current_thread()
    print("last time: {},当前线程ID: {},线程名称: {}".format(
        time.time() - threadStartTime, currentThread_G.ident, currentThread_G.name))
    # 这里其实有三个线程,线程1和线程2以及主线程,然后三个线程是并行执行的,所以主线程这里如果没有特殊
    # 操作的话,会立马执行完毕,然后上面两个线程再执行完成.

    # 守护线程的概念 ,如果将创建的线程设置为守护线程的意思
    """
    守护线程: 创建的子线程是主线程的守护线程,设置守护线程的意义就是主线程结束之后,会自动结束守护线程.
                    守护线程的周期和创建它的主线程的守护周期是一致的.
    """

    """
    join方法的意义: 阻塞当前的主线程,直到等待join的子线程结束之后才会继续往下执行.
    """
    detailUrlThread.join()
    detailHtmlThread.join()
    print("join 之后 last time: {},当前线程ID: {},线程名称: {}".format(
        time.time() - threadStartTime, currentThread_G.ident, currentThread_G.name))
